﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Zero.UControl.Base.Helper.Base
{
    internal class DrawArrowCommand : IDrawingCommand
    {
        public Path? Arrow { get; private set; }
        private readonly Canvas _canvas;
        private readonly Point _s;
        private readonly Point _e;
        private readonly Brush _stroke;
        private readonly double _thick;

        public DrawArrowCommand(Canvas c, Point s, Point e, Brush st, double t)
        {
            _canvas = c;
            _s = s;
            _e = e;
            _stroke = st;
            _thick = t;
        }

        public void Execute()
        {
            var g = new PathGeometry();
            var fig = new PathFigure { StartPoint = _s, IsClosed = false };
            fig.Segments.Add(new LineSegment(_e, true));
            g.Figures.Add(fig);
            // 箭头头部
            Vector dir = _s - _e;
            dir.Normalize();
            Vector ortho = new(-dir.Y, dir.X);
            double len = 12, w = 7;
            Point p1 = _e + dir * len + ortho * w;
            Point p2 = _e + dir * len - ortho * w;
            var tri = new PathFigure { StartPoint = _e, IsClosed = true };
            tri.Segments.Add(new LineSegment(p1, true));
            tri.Segments.Add(new LineSegment(p2, true));
            g.Figures.Add(tri);
            Arrow = new Path { Data = g, Stroke = _stroke, Fill = _stroke, StrokeThickness = _thick };
            _canvas.Children.Add(Arrow);
        }
        public void UnExecute()
        {
            if (Arrow != null)
                _canvas.Children.Remove(Arrow);
        }
    }
}
